package com.mjfeed.controller.admin;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mjfeed.controller.BaseController;
import com.mjfeed.entity.Comment;
import com.mjfeed.service.CommentService;
import com.mjfeed.util.Format;
import com.mjfeed.util.Pagination;
import com.mjfeed.util.ParamBean;

/**
 * @description 评价
 * @author zhaominglei
 * @date 2014年12月28日
 */
@Controller("adminCommentController")
@RequestMapping("/admin/comment")
public class CommentController extends BaseController {
	@Resource(name="commentService")
	private CommentService commentService;

	@RequestMapping(value = "/list", method=RequestMethod.GET)
	public String list(ParamBean paramBean, HttpServletRequest request, HttpServletResponse response, Model model){
		paramBean.setUrl(getBasePath(request) + "admin/comment/list.shtml");
		int p = (Format.null2Zero(request.getParameter("p")) == 0) ? 1 : Format.null2Zero(request.getParameter("p"));
		paramBean.setPage(p);
		Pagination<Comment> pagination = commentService.getPagination(paramBean);
		request.setAttribute("pagination", pagination);
		return "admin/comment/list";
	}
	
	@RequestMapping(value = "/edit",method=RequestMethod.GET)
	public String edit(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("comment", commentService.getById(Comment.class, id));
		return "admin/comment/edit";
	}
	
	@RequestMapping(value = "/update",method=RequestMethod.POST)
	public String edit(Comment comment, HttpServletRequest request, HttpServletResponse response, Model model){
		if (comment == null || comment.getId() == null || comment.getId() < 1) {
			return ERROR_VIEW;
		}
		Comment pComment = commentService.getById(Comment.class, comment.getId());
		pComment.setIsShow(comment.getIsShow());
		commentService.update(pComment);
		return "redirect:/admin/comment/list.shtml";
	}
	
	@RequestMapping(value = "/view",method=RequestMethod.GET)
	public String view(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("comment", commentService.getById(Comment.class, id));
		return "admin/comment/view";
	}
}
